#ifndef TEXTURE_HPP
#define TEXTURE_HPP

#include <string>

#include "SDL.h"
#include "libmath.hpp"

namespace sdl_engine {

class Texture final {
public:
    static Texture Null;

    Texture(u32 format, int access, const Size& size);
    Texture(const std::string& filename);
    Texture(const std::string& filename, const Color& keyColor);
    Texture(SDL_Surface* surface);
    Texture(const Texture&) = delete;
    ~Texture();

    Texture& operator=(const Texture&) = delete;

    inline operator bool() const {
        return Valid();
    }

    bool Valid() const { return texture_ && size_.w && size_.h; }

    inline const Size& GetSize() const { return size_; }
    inline SDL_Texture* Raw() { return texture_; }

    inline SDL_BlendMode GetBlendMode() const {
        SDL_BlendMode mode;
        SDL_GetTextureBlendMode(texture_, &mode);
        return mode;
    }

private:
    SDL_Texture* texture_ = nullptr;
    Size size_;
};

class Renderer;

class SubTexture final {
public:
    friend class Renderer;

    static SubTexture Null;

    SubTexture(Texture* texture, const Rect& rect);

    inline operator bool() const {
        return texture_;
    }

    Texture* GetTexture() const { return texture_; }
    inline const Size& GetSize() const { return rect_.size; }

private:
    Texture* texture_ = nullptr;
    Rect rect_;
};

}

#endif
